{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1、先导入为们需要的模块\n",
    "import requests\n",
    "api_secret = \"Vr0PtRCw-ZFwYXTKVfi6aDNaSqlfunK3\"\n",
    "\n",
    "# 2、输入我们API_Key\n",
    "api_key = 'jRpCaZ34kqNYJ8Zppdc-yGGum_YkETov'  # Replace with a valid Subscription Key here.\n",
    "\n",
    "\n",
    "# 3、目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url =  'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=498589869,3617162339&fm=26&gp=0.jpg'\n",
    "\n",
    "# 4、沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、准备symbol ? 后面的数据\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=498589869,3617162339&fm=26&gp=0.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1603550474,6dd5cc0e-e9f0-440c-840c-3a25f50116e6\",\"time_used\":163,\"faces\":[{\"face_token\":\"c88d6989f8f1f1aa96c45aae599907d5\",\"face_rectangle\":{\"top\":103,\"left\":192,\"width\":126,\"height\":126},\"attributes\":{\"gender\":{\"value\":\"Male\"},\"age\":{\"value\":34},\"smile\":{\"value\":2.752,\"threshold\":50.000},\"emotion\":{\"anger\":1.968,\"disgust\":4.195,\"fear\":0.798,\"happiness\":5.395,\"neutral\":9.955,\"sadness\":11.543,\"surprise\":66.147}}}],\"image_id\":\"el1ZyMgAo9urBuUjx1Zetg==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603550474,6dd5cc0e-e9f0-440c-840c-3a25f50116e6',\n",
       " 'time_used': 163,\n",
       " 'faces': [{'face_token': 'c88d6989f8f1f1aa96c45aae599907d5',\n",
       "   'face_rectangle': {'top': 103, 'left': 192, 'width': 126, 'height': 126},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 34},\n",
       "    'smile': {'value': 2.752, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 1.968,\n",
       "     'disgust': 4.195,\n",
       "     'fear': 0.798,\n",
       "     'happiness': 5.395,\n",
       "     'neutral': 9.955,\n",
       "     'sadness': 11.543,\n",
       "     'surprise': 66.147}}}],\n",
       " 'image_id': 'el1ZyMgAo9urBuUjx1Zetg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_token</th>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <th>attributes.age.value</th>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>c88d6989f8f1f1aa96c45aae599907d5</td>\n",
       "      <td>103</td>\n",
       "      <td>192</td>\n",
       "      <td>126</td>\n",
       "      <td>126</td>\n",
       "      <td>Male</td>\n",
       "      <td>34</td>\n",
       "      <td>2.752</td>\n",
       "      <td>50.0</td>\n",
       "      <td>1.968</td>\n",
       "      <td>4.195</td>\n",
       "      <td>0.798</td>\n",
       "      <td>5.395</td>\n",
       "      <td>9.955</td>\n",
       "      <td>11.543</td>\n",
       "      <td>66.147</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         face_token  face_rectangle.top  face_rectangle.left  \\\n",
       "0  c88d6989f8f1f1aa96c45aae599907d5                 103                  192   \n",
       "\n",
       "   face_rectangle.width  face_rectangle.height attributes.gender.value  \\\n",
       "0                   126                    126                    Male   \n",
       "\n",
       "   attributes.age.value  attributes.smile.value  attributes.smile.threshold  \\\n",
       "0                    34                   2.752                        50.0   \n",
       "\n",
       "   attributes.emotion.anger  attributes.emotion.disgust  \\\n",
       "0                     1.968                       4.195   \n",
       "\n",
       "   attributes.emotion.fear  attributes.emotion.happiness  \\\n",
       "0                    0.798                         5.395   \n",
       "\n",
       "   attributes.emotion.neutral  attributes.emotion.sadness  \\\n",
       "0                       9.955                      11.543   \n",
       "\n",
       "   attributes.emotion.surprise  \n",
       "0                       66.147  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.json_normalize(results,record_path='faces')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_face_rectangle = df[['face_token','face_rectangle.top','face_rectangle.left','face_rectangle.width','face_rectangle.height']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>face_token</th>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <th>face_rectangle.height</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>c88d6989f8f1f1aa96c45aae599907d5</td>\n",
       "      <td>103</td>\n",
       "      <td>192</td>\n",
       "      <td>126</td>\n",
       "      <td>126</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         face_token  face_rectangle.top  face_rectangle.left  \\\n",
       "0  c88d6989f8f1f1aa96c45aae599907d5                 103                  192   \n",
       "\n",
       "   face_rectangle.width  face_rectangle.height  \n",
       "0                   126                    126  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_face_rectangle"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
